import unittest
import requests
import json

from api.login import LoginAPI
from parameterized import parameterized

import app

def build_data():
    test_data = []
    file = app.BASE_DIR + "/data/login.json"
    with open(file, encoding="utf-8") as f:
        json_data = json.load(f)
        for case_data in json_data:
            username = case_data.get("username")
            password = case_data.get("password")
            verify_code = case_data.get("verify_code")
            status_code = case_data.get("status_code")
            status = case_data.get("status")
            msg = case_data.get("msg")
            test_data.append((username, password, verify_code, status_code, status, msg))
    print("test_data=".format(username, password, verify_code, status_code, status, msg))
    return test_data


class TestLogin2(unittest.TestCase):

    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.Session()

    @parameterized.expand(build_data())
    def test01_login(self, username, password, verify_code, status_code, status, msg):
        response1 = self.login_api.get_verify_code(self.session)
        self.assertEqual(status_code, response1.status_code)
        self.assertIn("image", response1.headers.get("Content-Type"))

        response2 = self.login_api.login(self.session, username, password, verify_code)
        print(response2.json())
        self.assertEqual(status_code, response2.status_code)
        self.assertEqual(status, response2.json().get("status"))
        self.assertIn(msg, response2.json().get("msg"))

    def tearDown(self):
        if self.session:
            self.session.close()











